import axios from "axios";
import { ElMessage } from "element-plus";

const http = axios.create({
  baseURL: import.meta.env.VITE_freeApi,
  timeout: 5000,
});

//请求拦截器
http.interceptors.request.use(
  (res) => {
    return res;
  },
  (error) => {
    return Promise.reject(error);
  }
);

//响应拦截器
http.interceptors.response.use(
  (res) => {
    if (res.status == 200) {
      return Promise.resolve(res.data);
    }
    if ((res.status = 404)) {
      return Promise.reject(res);
    }
    if (res.status == 500) {
      return ElMessage.error("服务异常");
    }
  },
  (err) => {
    return Promise.reject(err);
  }
);

export default http;
